package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AgentUser;
import com.vf.admin.miaodong.entity.AgentUserWithdraw;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class AgentUserDao extends BaseDao<AgentUser> {

    public List<AgentUser> page(SplitPageBean splitPage, Map<String, Object> paraMap) {
        ArrayList<Object> paramList = new ArrayList<>();
        StringBuilder sbStr = new StringBuilder("select * from agent_user where 1=1");
        if (StringEx.isNotNull(paraMap.get("username"))) {
            sbStr.append(" and username = ? ");
            paramList.add(paraMap.get("username"));
        }

        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AgentUser.class, splitPage);
    }

    public AgentUser findUserByUserId(Integer userId) {
        String sql = "select * from agent_user where id = "+ userId;
        return this.queryObj(sql, new ArrayList<>(), AgentUser.class);
    }

    public AgentUser findUserByAreaIdNotMe(Integer areaId, Integer userId) {
        String sql = "select * from agent_user where id != "+ userId +
                " and areaId = " + areaId;
        return this.queryObj(sql, new ArrayList<>(), AgentUser.class);
    }

    public AgentUser findUserByAreaId(Integer areaId) {
        String sql = "select * from agent_user where areaId = " + areaId;
        return this.queryObj(sql, new ArrayList<>(), AgentUser.class);
    }

    public void addMoney(Integer id, BigDecimal money) {
        String sql = " UPDATE agent_user SET money = money+ ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(money);
        paramList.add(id);
        this.update(sql, paramList);
    }

    public void subMoney(Integer id, BigDecimal money) {
        String sql = " UPDATE agent_user SET money = money- ?  WHERE id = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(money);
        paramList.add(id);
        this.update(sql, paramList);
    }

    public AgentUser findByUser(String username, String password) {
        List<Object> paramList = new ArrayList<>();
        StringBuilder sbstr = new StringBuilder(" select * from agent_user where 1=1 ");
        if (StringEx.isNotNull(username)) {
            sbstr.append(" and username = ? ");
            paramList.add(username);
        }
        if (StringEx.isNotNull(password)) {
            sbstr.append(" and password = ? ");
            paramList.add(password);
        }
        String sql = sbstr.toString();
        return this.queryObj(sql, paramList, AgentUser.class);
    }
}
